import { defineStore } from 'pinia'
import axios from '../utils/axios'
import router from '../router'

export const userStore = defineStore('user', {
  state: () => ({
    text: 'Hello Editor'
  }),
  actions: {

    test() {
      console.log('test')
    },

    async fetchMessage() {
      const response = await fetch('http://127.0.0.1:5173/message')
      const data = await response.json()
      this.message = data.message
    },

    // 登录
    async login(username, password) {
      const data = {
        username,
        password
      }
      const response = axios.post('/login', data)
      const res = await response
      console.log(res.data)
      if (res.data.code === 0) {
        router.push('/test')
      }
    }
  }

})
